package com.wocai.jrt.orgadmin.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.wocai.jrt.orgadmin.pay.bean.PaySerialResBean;

public class ExcelUtils {

	public static String exportExcel(List<PaySerialResBean> list) throws IOException {

		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();

		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("每日清算");

		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow(0);

		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("支付订单号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("银行名称");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("银行帐户名");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("银行卡号");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("金额（元）");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("状态");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("创建时间");
		cell.setCellStyle(style);

		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow((int) i + 1);
			PaySerialResBean psrb = list.get(i);

			// 第四步，创建单元格，并设置值
			row.createCell(0).setCellValue((double) (i + 1));
			row.createCell(1).setCellValue(psrb.getPaySerial());
			row.createCell(2).setCellValue(psrb.getBankDeposit());
			row.createCell(3).setCellValue(psrb.getBankName());
			row.createCell(4).setCellValue(psrb.getBankAccount());
			BigDecimal bd = new BigDecimal(psrb.getPayAmount());
			row.createCell(5).setCellValue(bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
			row.createCell(6).setCellValue("申请提现");

			cell = row.createCell(7);
			cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(psrb.getPayTime()));
		}

		// 第六步，将文件存到指定位置
		String excelPath = MyFileUtils.getFilePath(
				new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls",
				MyFileUtils.EXCEL_TYPE);
		try {
			FileOutputStream fout = new FileOutputStream(excelPath);
			wb.write(fout);
			fout.close();
			wb.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return excelPath;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "86.64566666";
		BigDecimal bd = new BigDecimal(Double.parseDouble(str));
		System.out.println(bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
	}

}